
package com.the9.C9Alarm;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

public class Media {
    Context c;
    
    private Vibrator mVibrator;
    SoundPool mSoundPool ;
    private Map<Integer,Integer> soundList;
    private static final long[] sVibratePattern         = new long[] { 500, 500 };

    private Media(Context c) {
        this.c = c;
        mVibrator = (Vibrator) c.getSystemService(Context.VIBRATOR_SERVICE);
        
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setLooping(true);
        mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        soundList = new HashMap<Integer,Integer>();
    }
    
    public void loadSoundPoolRes(int resId,int key){
    	soundList.put(key, mSoundPool.load(c, resId, 0));
    }
    public void playSound(int key){
    	mSoundPool.play(soundList.get(key), 1.0f, 1.0f, 0, 0, 1.0f);
    }

    
    public static Media getInstance(Context c) {
        if (m == null)
            m = new Media(c);
        return m;

    }

    /* MediaPlayer对象 */
    private MediaPlayer mMediaPlayer = null;

    private static Media m;

   
    /* 音乐的路径 */
    private static final String MUSIC_PATH = new String("/sdcard/");

    // 停止按钮

    public void Stop() {
        /* 是否正在播放 */
        if (mMediaPlayer.isPlaying()) {
            mMediaPlayer.stop();
        //	mMediaPlayer.reset();
            
          
        }
        mMediaPlayer.release();
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setLooping(true);

        //cancel the vibrator
        mVibrator.cancel();
    }

    public void playMusic(String path, boolean vibrate) {
    	//Stop();
    	
        /* 重置MediaPlayer */
        // mMediaPlayer.reset();
        /* 设置要播放的文件的路径 */
    //	mMediaPlayer.release();
     	
        if (vibrate) {
            mVibrator.vibrate(sVibratePattern, 0);
        }

        try {
        	
			mMediaPlayer.setDataSource( path );
			mMediaPlayer.prepare();
			
			
		} catch (Exception e) {
			Log.e("alarm", e.toString());
		}
        // mMediaPlayer.
        /* 准备播放 */
         
        /* 开始播放 */

        mMediaPlayer.start();

        // }catch (IOException e){}
    }
    public void playMusic(int res, boolean vibrate) {
    	//Stop();
    	
        /* 重置MediaPlayer */
        // mMediaPlayer.reset();
        /* 设置要播放的文件的路径 */
    //	mMediaPlayer.release();
     	
        if (vibrate) {
            mVibrator.vibrate(sVibratePattern, 0);
        }

        try {
        	mMediaPlayer.release();
			mMediaPlayer= MediaPlayer.create(c, res);
			mMediaPlayer.setLooping(true);
			//mMediaPlayer.prepare();
			
			
		} catch (Exception e) {
			Log.e("alarm", e.toString());
		}
        // mMediaPlayer.
        /* 准备播放 */
         
        /* 开始播放 */

        mMediaPlayer.start();

        // }catch (IOException e){}
    }

}

/* 过滤文件类型 */
class MusicFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        // 这里还可以设置其他格式的音乐文件
        return (name.endsWith(".mp3"));
    }
}
